﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Contains a word and a counter (occurences) for the word. Implements IComparable in order to sort the
/// words based on the occurrences value
/// </summary>
public class WordCounter : IComparable
{
    private int _occurrences;
    private string _name;

    /// <summary>
    /// Construtor. Creates a new instance of a WordCounter number of occurrences set to 0.
    /// </summary>
    /// <param name="name">Name of word as String</param>
    public WordCounter(string name)
    {
        this._name = name;
        this._occurrences = 0;
    }

    /// <summary>
    /// Increments the occurrence counter
    /// </summary>
    public void addOccurrence()
    {
        this._occurrences++;
    }

    /// <summary>
    /// Sets the occurrence counter
    /// </summary>
    /// <param name="occurrences">Number of occurrences</param>
    public void setOccurrences(int occurrences)
    {
        this._occurrences = occurrences;
    }

    /// <summary>
    /// Gets the name of the word
    /// </summary>
    /// <returns>Name of the word as String</returns>
    public string getName()
    {
        return this._name;
    }

    /// <summary>
    /// Gets the number of occurrences
    /// </summary>
    /// <returns>Number of occurrences as Int</returns>
    public int getOccurrences()
    {
        return this._occurrences;
    }

    /// <summary>
    /// Determines if two WordCounter instances are equal. Equality is determined if the name is the same.
    /// </summary>
    /// <param name="obj">Called with another WordCounter object</param>
    /// <returns>Boolean true if equal, otherwise false</returns>
    public override bool Equals(System.Object obj)
    {
        if (obj == null)
        {
            return false;
        }

        WordCounter wc = obj as WordCounter;
        if ((System.Object)wc == null)
        {
            return false;
        }

        // Return true if the name matches:
        return (wc.getName() == this._name);
    }

    /// <summary>
    /// Determines equality between other WordCounter instances. Orders by number of occurrences.
    /// </summary>
    /// <param name="obj">Called with another WordCounter object</param>
    /// <returns>Returns int value for camparison.</returns>
    public int CompareTo(object obj)
    {
        int returnResult = 0;
        if (obj == null)
            returnResult = -1;

        WordCounter otherWord = obj as WordCounter;

        if (otherWord != null)
        {
            if (this._occurrences > otherWord._occurrences)
                returnResult = -1;
            else if (this._occurrences < otherWord._occurrences)
                returnResult = 1;
            else
                returnResult = 0;
        }

        return returnResult;
    }

    /// <summary>
    /// It is required to override GetHashCode since we are overriding CompareTo
    /// </summary>
    /// <returns>hash code as int</returns>
    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
}